#!/usr/bin/awk -f
# $Id: rfc2893.awk 9ce65bed8ef4 2012/08/26 02:14:51 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# __version__  = "$Revision: 9ce65bed8ef4 $"
# __author__   = "$Author: pgurumur $"
# __modified__ = "$Date: 2012-08-26 02:14:51Z $"

function 
replace(string) 
{
   retval = "";
   if (length(string)) {
      gsub(/ /, "", string);
      retval = string;
   }

   return retval;
}

BEGIN {
   if (ARGC <= 1) {
      printf("%s: need filename\n", ARGV[0]);
      exit 1;
   }

   acl = 0;
   cryIndex = 0;
   inspi = 0;
   outspi = 0;

   auth = "";
   cryName = "";
   enc = "";
   inauth = "";
   incipher = "";
   local = "";
   outauth = "";
   outcipher = "";
   peer = "";
   proto = "";
   transform = "";
}

/^!.*/ {
   next;
}

$1 == "crypto" {
   if ($2 == "ipsec") {
      transform = replace($4);
      encIdx = replace($5);
      split(encIdx, array, "-");
      enc = array[2];

      authIdx = replace($NF);
      split(authIdx, array, "-");
      auth = array[2];
   }
   else if ($2 == "map") {
      cryName = replace($3);
      cryIndex = $4;
   }
}

$1 == "access-list" {
   acl = $2;
   local = replace($6);
   peer = replace($NF);
}

$0 ~ /session-key/ {
   proto = replace($4);
   if ($3 == "inbound") {
      inspi = $5;
      incipher = replace($7);
      inauth = replace($NF);
   }
   else if ($3 == "outbound") {
      outspi = $5;
      outcipher = replace($7);
      outauth = replace($NF);
   }
}


